﻿using System.Collections.Generic;
using SharpObjects.Configuration;
using SharpObjects.Configuration.ConfigurationProviders;
using SharpObjects.Instantiation;
using SharpObjects.Interfaces;
using SharpObjects.RuleEngine;

namespace SharpRuleEngineTest
{
    public class ViewDescriptor : IXmlInitializable
    {
        public string Id { get; set; }
        public List<UiEvent> UiEvents { get; set; }

        public ViewDescriptor()
        {
            this.UiEvents = new List<UiEvent>();
        }
        
        public UiEvent FindEvent(string eventId)
        {
            foreach (UiEvent uiEvent in UiEvents)
            {
                if(uiEvent.Name == eventId)
                {
                    return uiEvent;
                }
            }

            return null;
        }

        #region IXmlInitializable Members

        public void Initialize(System.Xml.XmlElement initializationElement)
        {
            IConfigurationReader init = new XmlConfigurationProvider(initializationElement);

            this.Id = init.GetString("@id");

            ITypeDescriptor eventTypeDescriptor = new TypeDescriptor()
                                                      {
                                                          AssemblyPath = "SharpRuleEngineTest.exe",
                                                          TypeName = "SharpRuleEngineTest.UiEvent"
                                                      };
            UiEvent[] events = init.GetObjects<UiEvent>("ui_events/*", eventTypeDescriptor);
            this.UiEvents.Clear();
            this.UiEvents.AddRange(events);

            this.IsInitialized = true;
        }

        public bool IsInitialized { get; set; }

        #endregion
    }
}
